package org.example.basic.day05;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Demo异常练习 {
    public static void main(String[] args) {
        UserManageServiceImpl userManageService = new UserManageServiceImpl();
        try {
            userManageService.register(new User("admin", "123456"));
            userManageService.login(new User("", "123456"));
        } catch (RegisterUserException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }catch (LoginUserException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}

class UserManageServiceImpl implements UserManageService{
    @Override
    public void register(User user) throws RegisterUserException {
        if(user.getUsername().trim().isEmpty()||user.getUsername()==null){
            throw new RegisterUserException("注册的用户名不能为空");
        }
        if (user.getPassword().trim().isEmpty()||user.getPassword()==null){
            throw new RegisterUserException("注册的密码不能为空");
        }
    }

    @Override
    public void login(User user) throws LoginUserException {
        if(user.getUsername().trim().isEmpty()||user.getUsername()==null){
            throw new LoginUserException("登录的用户名不能为空");
        }
        if (user.getPassword().trim().isEmpty()||user.getPassword()==null){
            throw new LoginUserException("登录的密码不能为空");
        }
    }

    @Override
    public void select(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void add(User user) throws AddUserException {
        if (user == null) {
            throw new AddUserException("用户不能为空");
        }
        if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            throw new AddUserException("用户名不能为空或空白");
        }
    }

    @Override
    public void update(User user) throws UpdateUserException {
        if (user == null) {
            throw new UpdateUserException("用户不能为空");
        }
    }
}

interface UserManageService{
    /**
     * 注册
     * @param user
     * @throws UserException
     */
    void register(User user) throws RegisterUserException;
    void login(User user) throws LoginUserException;
    void select(String  name);
    void delete(String  name);
    void add(User user) throws AddUserException;
    void update(User user) throws UpdateUserException;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class User{
    private String username;
    private String password;
}

class UserException extends RuntimeException{
    public UserException(String message) {
        super(message);
    }
    public UserException() {
        super();
    }
}
//注册异常
class RegisterUserException extends UserException{
    public RegisterUserException(String message) {
        super(message);
    }
    public RegisterUserException() {
        super();
    }
}
//登录异常
class LoginUserException extends UserException{
    public LoginUserException(String message) {
        super(message);
    }
    public LoginUserException() {
        super();
    }
}
//增加异常
class AddUserException extends UserException{
    public AddUserException(String message) {
        super(message);
    }
    public AddUserException() {
        super();
    }
}
//修改异常
class UpdateUserException extends UserException{
    public UpdateUserException(String message) {
        super(message);
    }
    public UpdateUserException() {
        super();
    }
}
